#!/bin/bash

# function module

function get_zip_codes {
  local INPUT_XML_FILE=$1
  local C_HEADEND_ID="'"$2"'"
  local C_DEVICE_TYPE_CODE="'"$3"'" 
  local END_CHAR="'"$4"'"

  # for supporting zip codes with space: perl -pe 's/ /-/g' |perl -pe 's/-\|/ \|/g'
  xml sel -N x="urn:tva:metadata:2010" -t -m "//x:TransmissionEntity[contains(@id, $C_HEADEND_ID) and contains(@Device_Type_code, $C_DEVICE_TYPE_CODE)]/x:GeoLocations/x:GeoLocation" -v "concat(@id,' |', $END_CHAR)" -n $INPUT_XML_FILE |perl -pe 's/ /-/g' |perl -pe 's/-\|/ \|/g' |sort -k1,1
}

function get_headend_info {
  local LINEUP_FILE_XML=$1
  # If IS_NEW is not null, this lineup is new version.
  local IS_NEW=`xml sel -N x="urn:tva:metadata:2010" -t -m "//x:TransmissionEntity[1]" -v "@Device_Type_code" -n $LINEUP_FILE_XML`

  if [ -z $IS_NEW ]; then # old lineup
    xml sel -N x="urn:tva:metadata:2010" -t -m "//x:TransmissionEntity" -v "concat('|', @id, '|', x:OtherIdentifier[@type='Device_Type_code'], ' |', x:Title, '|', x:OtherIdentifier[@type='MSO_Name'], '|', x:OtherIdentifier[@type='MSO_ID'], '|', x:OtherIdentifier[@type='MSO_Type'],'|', x:OtherIdentifier[@type='Device_Type'],'|', x:GeoLocations/x:GeoLocation/@id)" -n $LINEUP_FILE_XML | sort -t "|" -k2,3
  else # new lineup
    xml sel -N x="urn:tva:metadata:2010" -t -m "//x:TransmissionEntity" -v "concat('|', @id, '|', @Device_Type_code, ' |', x:Title, '|', x:OtherIdentifier[@type='MSO_Name'], '|', x:OtherIdentifier[@type='MSO_ID'], '|', x:OtherIdentifier[@type='MSO_Type'],'|', x:OtherIdentifier[@type='Device_Type'],'|', x:GeoLocations/x:GeoLocation/@id)" -n $LINEUP_FILE_XML | sort -t "|" -k2,3
  fi
}

function get_schedule_channels {
  local SCHEDULE_CHANNEL_FILE=$1
  local LINEUP_CHANNEL_FILE=$2
  join $SCHEDULE_CHANNEL_FILE $LINEUP_CHANNEL_FILE
}

function get_channel_list_in_schedule_file {
  local SCHEDULE_FILE=$1
  xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceInformation" -v "concat(@serviceId, ' | ', x:Name, ' | (', x:ServiceLanguage, ') |')" -n $SCHEDULE_FILE |sort -u |sort -t "|" -k1,1 |perl -pe 's/\n/&\n/g'
}

function check_each_lineup {
  local LINEUP_FILE_XML=$1
  local HEADEND_ID=$2
  local HID="'"$HEADEND_ID"'"
  local DEV_TYPE="'"$3"'"

  #echo "$DEV_TYPE Type: $HID"
  local IS_NEW=`xml sel -N x="urn:tva:metadata:2010" -t -m "//x:TransmissionEntity[1]" -v "@Device_Type_code" -n $LINEUP_FILE_XML`

  if [ ! -z $IS_NEW ]; then # new lineup
    xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection[contains(x:ServiceOf/@crid,$HID) and contains(x:ServiceOf/@Device_Type_code,$DEV_TYPE)]/x:Services" -m "x:Service" -s A:N:U x:OtherIdentifier -v "concat(ancestor::x:ServiceCollection/x:ServiceOf/@crid, '|', ancestor::x:ServiceCollection/x:ServiceOf/@Device_Type_code, '|', x:OtherIdentifier[@type='LCN'],  '|', x:OtherIdentifier[@type='DVB_ONID'], '|', x:OtherIdentifier[@type='DVB_transportStream_ID'], '|', x:OtherIdentifier[@type='DVB_service_id'], '|', ancestor::x:ServiceCollection/x:OtherIdentifier[@type='DVB_frequency'], '|', @id, '|Name:', x:Name)" -n $LINEUP_FILE_XML | perl -pe 's/Name:\n/Name:/g' | perl -pe 's/Name:\s*/Name:/g' | perl -pe 's/^\s*\n//' | perl -pe 's/Name://g' |perl -pe 's/srid:\/\/bds.tv\///g'
  else
    xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection[contains(x:ServiceOf/@crid,$HID)]/x:Services" -m "x:Service" -s A:N:U x:OtherIdentifier -v "concat(ancestor::x:ServiceCollection/x:ServiceOf/@crid, '||', x:OtherIdentifier[@type='LCN'],  '|', x:OtherIdentifier[@type='DVB_ONID'], '|', x:OtherIdentifier[@type='DVB_transportStream_ID'], '|', x:OtherIdentifier[@type='DVB_service_id'], '|', ancestor::x:ServiceCollection/x:OtherIdentifier[@type='DVB_frequency'], '|', @id, '|Name:', x:Name)" -n $LINEUP_FILE_XML | perl -pe 's/Name:\n/Name:/g' | perl -pe 's/Name:\s*/Name:/g' | perl -pe 's/^\s*\n//' | perl -pe 's/Name://g' |perl -pe 's/srid:\/\/bds.tv\///g'
  fi
}

function check_schedule_channels {
  local SCHEDULE_FILE_CHANNELS=$1
  local LINEUP_FILE_NEW_XML=$2
  local LINEUP_FILE_OLD_XML=$3
  local LINEUP_FILE_ONLY_NEW=`basename $LINEUP_FILE_NEW_XML`
  local LINEUP_FILE_NEW="${LINEUP_FILE_ONLY_NEW%.*}"
  local LINEUP_FILE_ONLY_OLD=`basename $LINEUP_FILE_OLD_XML`
  local LINEUP_FILE_OLD="${LINEUP_FILE_ONLY_OLD%.*}"
  local HEADEND_ID=$4
  local DEVICE_TYPE_CODE=$5
  local OUTPUTDIR=$6

  cat $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ALL.txt |awk -F "|" '{print $8, "|", $9, "|*"}' |sort -u | sort -t "|" -k1,1 > $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_channels.txt
  cat $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ALL.txt |awk -F "|" '{print $8, "|", $9, "|#"}' |sort -u | sort -t "|" -k1,1 > $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_channels.txt

  get_schedule_channels $SCHEDULE_FILE_CHANNELS $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_channels.txt > $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_schedule.txt
  get_schedule_channels $SCHEDULE_FILE_CHANNELS $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_channels.txt > $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_schedule.txt

  echo "-----Added Channels with schedule---------"
  join $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_channels.txt $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_channels.txt -a 1 |grep "|\*$" > $OUTPUTDIR/${HEADEND_ID}_${DEVICE_TYPE_CODE}_added_channels.txt
  join $OUTPUTDIR/${HEADEND_ID}_${DEVICE_TYPE_CODE}_added_channels.txt $SCHEDULE_FILE_CHANNELS |sort -u

  echo "-----Deleted Channels with schedule---------"
  join $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_channels.txt $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_channels.txt -a 2 |grep -v "|\*" > $OUTPUTDIR/${HEADEND_ID}_${DEVICE_TYPE_CODE}_deleted_channels.txt
  join $OUTPUTDIR/${HEADEND_ID}_${DEVICE_TYPE_CODE}_deleted_channels.txt $SCHEDULE_FILE_CHANNELS |sort -u
}

function check_lcn_triplet_freq_data {
  local LINEUP_FILE_NEW_XML=$1
  local LINEUP_FILE_OLD_XML=$2
  local LINEUP_FILE_ONLY_NEW=`basename $LINEUP_FILE_NEW_XML`
  local LINEUP_FILE_NEW="${LINEUP_FILE_ONLY_NEW%.*}"
  local LINEUP_FILE_ONLY_OLD=`basename $LINEUP_FILE_OLD_XML`
  local LINEUP_FILE_OLD="${LINEUP_FILE_ONLY_OLD%.*}"
  local HEADEND_ID=$3
  local DEVICE_TYPE_CODE=$4
  local OUTPUTDIR=$5

  # get $3: LCN, $4: ONID $5: TSID $6:SID $7:Frequency $8: Channel id $9: Channel name
  cat $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ALL.txt |awk -F "|" '{print $3"-"$4"-"$5"-"$6"-"$7, $8, $9, "|*"}' |sort -u | sort -k1,1 > $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ltf_filtered.txt
  cat $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ALL.txt |awk -F "|" '{print $3"-"$4"-"$5"-"$6"-"$7, $8, $9, "|#"}' |sort -u | sort -k1,1 > $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ltf_filtered.txt

  echo "-----Added LCN or Triplet(with Frequency) Channels---------"
  join $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ltf_filtered.txt $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ltf_filtered.txt -a 1 |grep "|\*$" > $OUTPUTDIR/${HEADEND_ID}_added_lft_channels.txt
  cat $OUTPUTDIR/${HEADEND_ID}_added_lft_channels.txt

  echo "-----Deleted LCN or Triplet(with Frequency) Channels---------"
  join $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ltf_filtered.txt $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ltf_filtered.txt -a 2 |grep -v "|\*" > $OUTPUTDIR/${HEADEND_ID}_${DEVICE_TYPE_CODE}_deleted_ltf_channels.txt
  cat $OUTPUTDIR/${HEADEND_ID}_${DEVICE_TYPE_CODE}_deleted_ltf_channels.txt
}

function check_zipcode_mapping {
  local LINEUP_FILE_NEW_XML=$1
  local LINEUP_FILE_OLD_XML=$2
  local HEADEND_ID=$3
  local DEVICE_TYPE_CODE=$4
  local LINEUP_FILE_ONLY_NEW=`basename $LINEUP_FILE_NEW_XML`
  local LINEUP_FILE_NEW="${LINEUP_FILE_ONLY_NEW%.*}"
  local LINEUP_FILE_ONLY_OLD=`basename $LINEUP_FILE_OLD_XML`
  local LINEUP_FILE_OLD="${LINEUP_FILE_ONLY_OLD%.*}"
  local OUTPUTDIR=$5

  # Check for zip code mapping
  get_zip_codes $LINEUP_FILE_NEW_XML $HEADEND_ID $DEVICE_TYPE_CODE '*' > $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_zipcodes.txt
  get_zip_codes $LINEUP_FILE_OLD_XML $HEADEND_ID $DEVICE_TYPE_CODE '#' > $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_zipcodes.txt

  # for support zip codes that contain space like UK we use: perl -pe 's/-/ /g'
  echo "-----Added zip codes-------"
  join $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_zipcodes.txt $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_zipcodes.txt -a 1 | grep "|\*$" |perl -pe 's/-/ /g' > $OUTPUTDIR/${HEADEND_ID}_${DEVICE_TYPE_CODE}_added_zipcodes.txt
  cat $OUTPUTDIR/${HEADEND_ID}_${DEVICE_TYPE_CODE}_added_zipcodes.txt

  echo "-----Deleted zip codes-------"
  join $OUTPUTDIR/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_zipcodes.txt $OUTPUTDIR/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_zipcodes.txt -a 2 | grep -v "|\*" |perl -pe 's/-/ /g' > $OUTPUTDIR/${HEADEND_ID}_${DEVICE_TYPE_CODE}_deleted_zipcodes.txt
  cat $OUTPUTDIR/${HEADEND_ID}_${DEVICE_TYPE_CODE}_deleted_zipcodes.txt
}

function get_latest_MSO_file {
  local MSO_FILE_PTN=$1
  local COUNTRY=$2
  local LINEUP_DIR=$3
  # get latest file using version info
  ls ${LINEUP_DIR}/${COUNTRY}/${MSO_FILE_PTN}*.xml |sort -t "-" -k5,5 -n |tail -1
}

function get_latest_schedule_file {
  local COUNTRY=$1
  local SCHEDULE_DIR=$2
  local SCHEDULE_DATE=$3
  local OUTPUT_DIR=$4
  local SCHEDULE_ZIP_FILE=""
  local SCHEDULE_FILE=""

  if [ ! -d "${OUTPUTDIR}/${COUNTRY}" ]; then
    mkdir -p $OUTPUTDIR/${COUNTRY}
  fi

  if [ ! -f `ls ${SCHEDULE_DIR}/${COUNTRY}/${SCHEDULE_DATE}*.zip|sort|tail -1` ]; then
    echo "$COUNTRY (${SCHEDULE_DATE}): does not exist!"
  else
    SCHEDULE_ZIP_FILE=`ls ${SCHEDULE_DIR}/${COUNTRY}/${SCHEDULE_DATE}*.zip |sort |tail -1`
    if [ ! -f ${OUTPUT_DIR}/${COUNTRY}/${SCHEDULE_DATE}*.xml ]; then
      unzip $SCHEDULE_ZIP_FILE -d ${OUTPUT_DIR}/${COUNTRY} > /dev/null
    fi
    SCHEDULE_FILE=`ls ${OUTPUT_DIR}/${COUNTRY}/${SCHEDULE_DATE}*.xml |sort |tail -1`
    echo $SCHEDULE_FILE
  fi
}

function compare_channels_lineup_schedule {
  local SCHEDULE_FILE_CHANNELS=$1
  local LINEUP_FILE_XML=$2
  local HEADEND_ID=$3
  local DEVICE_TYPE_CODE=$4
  local OUTPUTDIR=$5
  local LINEUP_FILE_ONLY=`basename $LINEUP_FILE_XML`
  local LINEUP_FILE="${LINEUP_FILE_ONLY%.*}"

  cat $OUTPUTDIR/lineup_${LINEUP_FILE}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ALL.txt |awk -F "|" '{print $8, "|", $9, "|*"}' |sort -u | sort -t "|" -k1,1 > $OUTPUTDIR/lineup_${LINEUP_FILE}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_channels.txt

  get_schedule_channels $SCHEDULE_FILE_CHANNELS $OUTPUTDIR/lineup_${LINEUP_FILE}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_channels.txt > $OUTPUTDIR/lineup_${LINEUP_FILE}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_schedule.txt
}

function get_lang_stat_in_schedule_file {
  local SCHEDULE_FILE_CHANNELS=$1
  local LANG=""
  local NUM=""
  local OUTPUT=""
  # get unique Lang 
  for LANG  in `cat $SCHEDULE_FILE_CHANNELS | awk -F "|" '{print $3}' |sort -u`
  do
    NUM=`cat $SCHEDULE_FILE_CHANNELS |grep $LANG |wc -l`
    OUTPUT=$LANG": "$NUM" "$OUTPUT
  done
  echo $OUTPUT
}

function get_program_title_lang_list {
  local SCHEDULE_FILE_XML=$1
  xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ProgramInformation/x:BasicDescription" -v "concat(parent::x:ProgramInformation/@programId, '| ')" -m "x:Title[@type='main']" -v "concat(@xml:lang, '|')" -b -n $SCHEDULE_FILE_XML |sort -u |sort -k1,1
}

function get_program_list_by_channel {
  local SCHEDULE_FILE_XML=$1
  local PROGRAM_TITLE_LANG_FILE=$2
  local OUTPUTDIR=$3
  local CHANNEL_ID=""
#$SCHEDULE_FILE_XML ${OUTPUTDIR}/${COUNTRY}
  xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ProgramLocationTable/x:Schedule/x:ScheduleEvent/x:Program" -v "concat(ancestor::x:Schedule/@serviceIDRef, '|', @crid, '|', parent::x:ScheduleEvent/x:PublishedStartTime, '|', parent::x:ScheduleEvent/x:PublishedDuration)" -n $SCHEDULE_FILE_XML > ${OUTPUTDIR}/programs_by_channel_all.txt

  for CHANNEL_ID in `cat ${OUTPUTDIR}/programs_by_channel_all.txt |awk -F "|" '{print $1}' |perl -pe 's/ //g' |sort -u`
  do
    cat ${OUTPUTDIR}/programs_by_channel_all.txt | grep "^${CHANNEL_ID}" |awk -F "|" '{print $2"|"}' |sort -u > ${OUTPUTDIR}/${CHANNEL_ID}_programs_list.txt
    join ${OUTPUTDIR}/${CHANNEL_ID}_programs_list.txt $PROGRAM_TITLE_LANG_FILE -a 1 > ${OUTPUTDIR}/${CHANNEL_ID}_programs_list_with_lang.txt
  done
}

function get_channel_program_stat {
  local SCHEDULE_FILE_CHANNELS=$1
  local COUNTRY=$2
  local OUTPUTDIR=$3
  local CHANNEL_ID=""
  local CHANNEL_NAME=""
  local CHANNEL_LANG=""
  local MATCH_LINE=""
  local PROGRAM_STAT_BY_CHANNEL_FILE=${OUTPUTDIR}/${COUNTRY}-program_title_lang_stat_by_channel.txt
  local NUM_PROGRAMS=""
  local NUM_LANG_PROGRAMS=""

  if [ -f $PROGRAM_STAT_BY_CHANNEL_FILE ]; then
    rm $PROGRAM_STAT_BY_CHANNEL_FILE
  fi
  echo "||COUNTRY||CHANNEL ID||CHANNEL NAME||BUILD LANG||TOTAL PROGRAMS||BUILD LANG PROGRAMS||" > $PROGRAM_STAT_BY_CHANNEL_FILE

  for CHANNEL_ID in `cat $SCHEDULE_FILE_CHANNELS | awk -F "|" '{print $1}' | perl -pe 's/ //g'`
  do
    MATCH_LINE=`cat $SCHEDULE_FILE_CHANNELS | grep "^$CHANNEL_ID"`
    CHANNEL_NAME=`echo $MATCH_LINE | awk -F "|" '{print $2}' | perl -pe 's/^ //g' |perl -pe 's/ \n/\n/g'`
    CHANNEL_LANG=`echo $MATCH_LINE | awk -F "|" '{print $3}' | perl -pe 's/ \(//g' |perl -pe 's/\) //g'`
    NUM_PROGRAMS=`cat ${OUTPUTDIR}/${COUNTRY}/${CHANNEL_ID}_programs_list_with_lang.txt |wc -l`
    NUM_LANG_PROGRAMS=`cat ${OUTPUTDIR}/${COUNTRY}/${CHANNEL_ID}_programs_list_with_lang.txt |grep $CHANNEL_LANG|wc -l`
    echo "|"$COUNTRY"|"$CHANNEL_ID"|"$CHANNEL_NAME"|"$CHANNEL_LANG"|"$NUM_PROGRAMS"|"$NUM_LANG_PROGRAMS"|" >> $PROGRAM_STAT_BY_CHANNEL_FILE
  done

}
